package com.lgj.springboot_lgj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.Article;
import com.lgj.springboot_lgj.bean.basic.BaseUser;
import com.lgj.springboot_lgj.bean.basic.Category;
import com.lgj.springboot_lgj.bean.basic.Comment;
import com.lgj.springboot_lgj.bean.ext.ArticleExt;
import com.lgj.springboot_lgj.dao.basic.ArticleMapper;
import com.lgj.springboot_lgj.dao.basic.BaseUserMapper;
import com.lgj.springboot_lgj.dao.basic.CategoryMapper;
import com.lgj.springboot_lgj.dao.ext.ArticleExtMapper;
import com.lgj.springboot_lgj.dao.ext.CommentMapperExt;
import com.lgj.springboot_lgj.exception.CustomerException;
import com.lgj.springboot_lgj.service.IArticleService;
import com.lgj.springboot_lgj.service.ICommentService;
import com.lgj.springboot_lgj.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author liguanghui
 * @Date 2022/6/19
 */
@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper mapper;
    @Autowired
    private ArticleExtMapper ExtMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BaseUserMapper baseUserMapper;
    @Autowired
    private CommentMapperExt commentMapperExt;
    @Autowired
    private ICommentService commentService;
    //根据id查询
    @Override
    public ArticleExt findByIdWithCategoryAndUser(int id) {
        ArticleExt articleExt = ExtMapper.findByIdWithCategoryAndUser(id);
        if(articleExt == null){
            throw new CustomerException("咨询不存在");
        }
        return articleExt;
    }
    //修改或者新增咨询
    @Override
    public void saveOrUpdate(Article article) {
        //当添加的咨询的title重复时，提示用户无法添加重复的title
        System.out.println(article.getArticleTitle());
        Article a = ExtMapper.findArticleExtTitle(article.getArticleTitle());
        System.out.println(a);
        if(a != null){
            throw new CustomerException("题目重复,请修改");
        }
        //当添加的咨询的title重复时，提示用户无法添加重复的title
        /*List titleList = ExtMapper.findArticleTitle();
        String title = null;
        for (int i = 0; i < titleList.size(); i++) {
            title = (String) titleList.get(i);
            if(article.getArticleTitle().equals(title)){
                throw new CustomerException("题目重复，请修改");
            }
        }*/
        //当添加咨询的url重复时，提示用户不能添加
        List coverList = ExtMapper.findArticleCover();
        String cover = null;
        for (int i = 0; i < coverList.size(); i++) {
            cover = (String) coverList.get(i);
            if (article.getArticleCover().equals(cover)){
                throw  new CustomerException("url重复,请修改");
            }
        }
        //外键
        Category category = categoryMapper.selectByPrimaryKey(article.getArticleCategoryId());
        if(category == null){
            throw new CustomerException("栏目id无效");
        }
        BaseUser baseUser = baseUserMapper.selectByPrimaryKey(article.getArticleUserId());
        if(baseUser == null){
            throw new CustomerException("user_id无效");
        }
        //有id执行修改
        if(article.getArticleId() !=null){
            Article articleTest = mapper.selectByPrimaryKey(article.getArticleId());
            if(articleTest ==null){
                throw new CustomerException("咨询不存在，无法修改");
            }
            mapper.updateByPrimaryKeySelective(article);
        }
        //无id执行插入
        if(article.getArticleId() == null){
            article.setArticleReadTimes(0);
            article.setArticleStatus(0);
            article.setArticleThumpUp(0);
            article.setArticlePublishTime(new Date());
            mapper.insertSelective(article);
        }
    }
    //查询全部+模糊查询
    @Override
    public PageInfo<ArticleExt> pageQueryArticleWithCategoryAndUser(int pageNum, int pageSize, String title, Integer status, Integer categoryId) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<ArticleExt> list = ExtMapper.findArticleWithCategoryAndUser(pageNum,pageSize,title,status,categoryId);
//        Collections.reverse(list);
        PageInfo<ArticleExt> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //删除
    @Override
    public void deleteById(int id) {
        Article article = mapper.selectByPrimaryKey(id);
        if (article == null){
            throw new CustomerException("咨询不存在");
        }
        Map<String, Object> userInfo = UserInfoUtil.getUserInfo();
        Object role = userInfo.get("role");
        Object username = userInfo.get("username");
        Integer userRole = (Integer) role;
        if(userRole == 1){
            Integer articleUserId = article.getArticleUserId();
            BaseUser baseUser = baseUserMapper.selectByPrimaryKey(articleUserId);
            if(baseUser.getUsername().equals(username)){
                ExtMapper.deleteById(id);
            }else {
                throw new CustomerException("用户无法删除咨询状态");
            }
        }
        int i = 1;
        while (i==1){
            List<Comment> commentList = commentMapperExt.findAllCommentByArtId(id);
            if(commentList != null && commentList.size()>0){
                Comment comment = commentList.get(0);
                commentService.deleteById(comment.getCommentId());
//            ExtMapper.deleteById(id);
            }else {
                i = 0;
            }
        }
        ExtMapper.deleteById(id);
    }
    //查询推荐
    @Override
    public PageInfo<ArticleExt> pageQueryRecommend(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<ArticleExt> list = ExtMapper.findRecommendAll();
        PageInfo<ArticleExt> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //修改状态
    @Override
    public void changeStatus(int id, int status) {
        Article article = mapper.selectByPrimaryKey(id);
        if(article == null){
            throw new CustomerException("咨询不存在");
        }
        Map<String, Object> userInfo = UserInfoUtil.getUserInfo();
        Object role = userInfo.get("role");
        Integer userRole = (Integer) role;
        if(userRole == 1){
            throw new CustomerException("用户无法修改咨询状态");
        }
        if(status == 1 || status == 0 || status == 2 || status == 3){
            ExtMapper.changeStatus(id,status);
        }else {
            throw new CustomerException("状态码不符合,状态：0-待审核，1-不通过，2-通过，3-推荐");
        }
    }
}
